//
//  KHHSearchViewController.m
//  Travel
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "KHHSearchViewController.h"
#import "KHHSearchTableViewCell.h"
#import "KHHRecommendModel.h"
#import "KHHTravelNotesDetailPageViewController.h"
#import "KHHSearchNilPage.h"
#import "CXRecommendViewController.h"

@interface KHHSearchViewController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UISearchBar *searchBar;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)UIView *titleView;
@property (nonatomic, retain)NSMutableArray *dataArr; //保存总数据

@end

@implementation KHHSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
 // self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"leftBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backToLast)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"leftBack"] style:UIBarButtonItemStylePlain target:self action:@selector(backToLast)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    [self addScreenEdgePan];
    [self SearchBar];
    [self createTableView];
    [self getData:self.text];
}

#pragma mark - 视图将要出现时候
- (void)viewWillAppear:(BOOL)animated
{
    [self.searchBar resignFirstResponder];
}

#pragma mark - 视图要消失时候
- (void)viewWillDisappear:(BOOL)animated
{
    [self.searchBar resignFirstResponder];
}

#pragma mark - 创建searchBar
- (void)SearchBar
{
    // 初始化
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(60, 5, WIDTH - 80 , 30)];
    // 设置代理
    self.searchBar.delegate = self;
    //类型
    [self.searchBar setBarStyle:UIBarStyleDefault];
    //提示文本
    self.searchBar.placeholder = @"搜索目的地  游记...";
    self.searchBar.text = self.text;
    
    //键盘类型
    [self.searchBar setKeyboardType:UIKeyboardTypeDefault];
    [self.searchBar becomeFirstResponder];
    //视图剪切
    self.searchBar.layer.masksToBounds = YES;
    self.searchBar.layer.cornerRadius = 10;
    
    //关闭用户交互
    self.searchBar.userInteractionEnabled = YES;
    [self.navigationController.navigationBar addSubview:self.searchBar];
    
}

#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CWIDTH, CHEIGHT ) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    //注册cell
    [self.tableView registerClass:[KHHSearchTableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

#pragma mark - 按钮触发方法
- (void)backToLast
{
    [self.navigationController popViewControllerAnimated:YES];
    [self.searchBar removeFromSuperview];
}

#pragma mark - searchBar 触发方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
    [self getData:self.searchBar.text];
}

#pragma mark - 获取搜索数据
- (void)getData:(NSString *)text
{
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/search/?key=%@&start=0&count=20&source=search",text];
    
    // 中文转码
    NSString *urlStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    [CXNetHandle getUrl:urlStr cookie:nil result:^(id result) {
        
        self.dataArr =  [KHHRecommendModel transformWithArray:result[@"trips"]];
        
        if (self.dataArr.count != 0) {
            [self createTableView];
            
        }
        //else {
//            KHHSearchNilPage *nilPage = [[KHHSearchNilPage alloc] init];
//            [self.navigationController pushViewController:nilPage animated:YES];
//        }
    }];
    
}


#pragma mark - tableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KHHSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
    cell.str = self.searchBar.text;
    cell.str = self.text;
    cell.recommentModel = self.dataArr[indexPath.row];
    
    return cell;
    
}

#pragma mark - 点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KHHTravelNotesDetailPageViewController *detail = [[KHHTravelNotesDetailPageViewController alloc]init];
    detail.locationImpressionToDetailID = [self.dataArr[indexPath.row] notesId];
    
    [self.navigationController pushViewController:detail animated:YES];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

#pragma mark-滑动手势1
- (void)addScreenEdgePan
{
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(presentLeftMenuViewController:)];
    screenEdgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePan];
    [self.tableView addGestureRecognizer:screenEdgePan];

}
#pragma mark-滑动手势2
-(void)presentLeftMenuViewController:(UIScreenEdgePanGestureRecognizer *)edge
{
    [self.navigationController popViewControllerAnimated:YES];
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
